package com.huatai.datadevelop.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author hecai
 * @description: TODO
 * @date 2022/11/21 19:19
 * @Version 1.0
 */
@Service
public class TemplateService {

    @Autowired
    protected RestTemplate restTemplate;

    /**
     * get请求
     *
     * @param url       请求地址
     * @param headerMap 头部信息
     * @param resp      响应结果类型
     * @return
     */
    public synchronized Object get(String url, Map<String, String> headerMap, Class<?> resp) {
        HttpHeaders httpHeaders = this.commonHeader();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, resp);
        return result.getBody();
    }

    /**
     * post请求
     * @param url  请求地址
     * @param headerMap  头部信息
     * @param resp  响应结果类型
     * @return
     */
    public synchronized Object postByExchange(String url, Map<String, String> headerMap, Class<?> resp) {
        HttpHeaders httpHeaders = this.commonHeader();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, resp);
        return result.getBody();
    }

    /**
     * post请求
     * @param url  请求地址
     * @param headerMap  头部信息
     * @param t  body 类型
     * @param resp  响应结果类型
     * @param <T>
     * @return
     */
    public synchronized <T> Object postByModelExchange(String url, Map<String, String> headerMap,T t, Class<?> resp) {
        HttpHeaders httpHeaders = this.commonHeader();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        httpHeaders.add("Content-Type","application/json");
        HttpEntity httpEntity = new HttpEntity(t,httpHeaders);
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, resp);
        return result.getBody();
    }

    /**
     * post 请求
     *
     * @param url        请求地址
     * @param headerMap  头信息
     * @param jsonObject 请求参数 JSON
     * @return JSONObject
     */
    public synchronized JSONObject post(String url, Map<String, String> headerMap, JSONObject jsonObject) {
        HttpHeaders httpHeaders = this.commonHeader();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
        JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        return result;
    }

    /**
     * put请求
     * @param url  请求地址
     * @param headerMap  头部信息
     * @param t  body 类型
     * @param resp  响应结果类型
     * @param <T>
     * @return
     */
    public synchronized <T> Object putByModelExchange(String url, Map<String, String> headerMap,T t, Class<?> resp) {
        HttpHeaders httpHeaders = this.commonHeader();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        HttpEntity httpEntity = new HttpEntity(t,httpHeaders);
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, resp);
        return result.getBody();
    }

    /**
     * post请求
     * @param url  请求地址
     * @param headerMap  头部信息
     * @param t  body 类型
     * @param resp  响应结果类型
     * @param <T>
     * @return
     */
    public synchronized <T> Object postByModelExchangeForm(String url, Map<String, String> headerMap,Map<String,Object> bodyMap, Class<?> resp) {
        // 构建请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
            httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
        // 封装请求参数
        LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<>();
        for (Map.Entry<String, Object> body : bodyMap.entrySet()) {
            linkedMultiValueMap.add(body.getKey(), body.getValue());
        }
        // 构建请求体
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(linkedMultiValueMap, httpHeaders);
        // 发起http请求, 以字节流接收响应信息, 方便解码特殊的响应头(例如响应头为"Content-Language/zh-CN")
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, resp);
        return result.getBody();
    }

    /**
     * 获取公用部分
     * @return
     */
    public HttpHeaders commonHeader(){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("X-Content-Type-Options","nosniff");
        httpHeaders.add("X-Frame-Options","DENY");
        httpHeaders.add("X-XSS-Protection","1;mode=block");
        return httpHeaders;
    }
}
